package cn.iocoder.yudao.module.shop.controller.admin.crossorder;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.crossorder.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.crossorder.ShopCrossOrderDO;
import cn.iocoder.yudao.module.shop.convert.crossorder.ShopCrossOrderConvert;
import cn.iocoder.yudao.module.shop.service.crossorder.ShopCrossOrderService;

@Tag(name = "管理后台 - 跨店铺")
@RestController
@RequestMapping("/shop/cross-order")
@Validated
public class ShopCrossOrderController {

    @Resource
    private ShopCrossOrderService crossOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建跨店铺")
    @PreAuthorize("@ss.hasPermission('shop:cross-order:create')")
    public CommonResult<Long> createCrossOrder(@Valid @RequestBody ShopCrossOrderCreateReqVO createReqVO) {
        return success(crossOrderService.createCrossOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨店铺")
    @PreAuthorize("@ss.hasPermission('shop:cross-order:update')")
    public CommonResult<Boolean> updateCrossOrder(@Valid @RequestBody ShopCrossOrderUpdateReqVO updateReqVO) {
        crossOrderService.updateCrossOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨店铺")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:cross-order:delete')")
    public CommonResult<Boolean> deleteCrossOrder(@RequestParam("id") Long id) {
        crossOrderService.deleteCrossOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨店铺")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:cross-order:query')")
    public CommonResult<ShopCrossOrderRespVO> getCrossOrder(@RequestParam("id") Long id) {
        ShopCrossOrderDO crossOrder = crossOrderService.getCrossOrder(id);
        return success(ShopCrossOrderConvert.INSTANCE.convert(crossOrder));
    }

    @PostMapping("/get-month")
    @Operation(summary = "获得跨店铺")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:cross-order:query')")
    public CommonResult<ShopCrossOrderMonthRespVO> getCrossOrder(@Valid @RequestBody ShopCrossOrderMonthReqVO reqVO) {
        ShopCrossOrderMonthRespVO respVO = new ShopCrossOrderMonthRespVO();
        DateTime time = new DateTime(reqVO.getTime());
        DateTime startTime = DateUtil.beginOfMonth(time);
        DateTime endTime = DateUtil.endOfMonth(time);
        respVO.setSourceList(crossOrderService.getSourceCrossOrderListByTenantId(reqVO.getTenantId(), startTime.toLocalDateTime(), endTime.toLocalDateTime()));
        respVO.setTargetList(crossOrderService.getTargetCrossOrderListByTenantId(reqVO.getTenantId(), startTime.toLocalDateTime(), endTime.toLocalDateTime()));

        return success(respVO);
    }

    @PostMapping("/confirm")
    @Operation(summary = "获得跨店铺")
    @PreAuthorize("@ss.hasPermission('shop:cross-order:update')")
    public CommonResult<Boolean> getCrossOrder(@Valid @RequestBody ShopCrossOrderConfirmReqVO reqVO) {
        return success(crossOrderService.confirm(reqVO.getTenantId(), reqVO.getId()));
    }

    @GetMapping("/list")
    @Operation(summary = "获得跨店铺列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:cross-order:query')")
    public CommonResult<List<ShopCrossOrderRespVO>> getCrossOrderList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopCrossOrderDO> list = crossOrderService.getCrossOrderList(ids);
        return success(ShopCrossOrderConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨店铺分页")
    @PreAuthorize("@ss.hasPermission('shop:cross-order:query')")
    public CommonResult<PageResult<ShopCrossOrderRespVO>> getCrossOrderPage(@Valid ShopCrossOrderPageReqVO pageVO) {
        PageResult<ShopCrossOrderDO> pageResult = crossOrderService.getCrossOrderPage(pageVO);
        return success(ShopCrossOrderConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨店铺 Excel")
    @PreAuthorize("@ss.hasPermission('shop:cross-order:export')")
    @OperateLog(type = EXPORT)
    public void exportCrossOrderExcel(@Valid ShopCrossOrderExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopCrossOrderDO> list = crossOrderService.getCrossOrderList(exportReqVO);
        // 导出 Excel
        List<ShopCrossOrderExcelVO> datas = ShopCrossOrderConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "跨店铺.xls", "数据", ShopCrossOrderExcelVO.class, datas);
    }

}
